package com.zzh.custom.chess;

import android.util.SparseArray;

public class Pieces {


    public static boolean isKill() {

        return false;
    }

    public static int isTookHold(int preKey, int nowKey, SparseArray<PiecesBean> piecesBeanSparseArray) {
        return -1;
    }


    /**
     * 判断是不是允许执行
     *
     * @param nowKey                当前的key
     * @param targetKey             目标的key
     * @param piecesBeanSparseArray 现在的数据集合
     * @return 是不是允许执行
     */
    public static boolean isAllowPerform(int nowKey, int targetKey, SparseArray<PiecesBean> piecesBeanSparseArray) {
        PiecesBean piecesBean = piecesBeanSparseArray.get(nowKey);
        if (piecesBean == null) {
            return false;
        }
        PiecesBean targetPiecesBean = piecesBeanSparseArray.get(targetKey);
        if (targetPiecesBean != null) {
            if (targetPiecesBean.getCampType() == piecesBean.getCampType()) {
                return false;
            }
        }
        boolean isStatus = false;
        switch (piecesBean.type) {
            case PiecesBean.AS:
                isStatus = judgeAs(piecesBean, targetKey, piecesBeanSparseArray);
                break;
            case PiecesBean.CANNON:
                isStatus = judgeCannon(piecesBean, targetKey, piecesBeanSparseArray);
                break;
            case PiecesBean.HORSE:
                isStatus = judgeHorse(piecesBean, targetKey, piecesBeanSparseArray);
                break;
            case PiecesBean.PAWN:
                isStatus = judgePawn(piecesBean, targetKey, piecesBeanSparseArray);
                break;
            case PiecesBean.SCHOLAR:
                isStatus = judgeScholar(piecesBean, targetKey, piecesBeanSparseArray);
                break;
            case PiecesBean.TAKE:
                isStatus = judgeTake(piecesBean, targetKey, piecesBeanSparseArray);
                break;
            case PiecesBean.VEHICLE:
                isStatus = judgeVehicle(piecesBean, targetKey, piecesBeanSparseArray);
                break;
        }
        return isStatus;
    }


    // 车
    private static boolean judgeVehicle(PiecesBean piecesBean, int targetKey, SparseArray<PiecesBean> piecesBeanSparseArray) {
        int key = piecesBean.getPointKey();
        int x = key / 100;
        int y = key % 100;
        int targetX = targetKey / 100;
        int targetY = targetKey % 100;
        if (x != targetX && y != targetY) {
            return false;
        }
        if (y == targetY) {
            if (x > targetX) {
                for (int i = targetX + 1; i < x; i++) {
                    PiecesBean bean = piecesBeanSparseArray.get(i * 100 + y);
                    if (bean != null) {
                        return false;
                    }
                }
            } else {
                for (int i = x + 1; i < targetX; i++) {
                    PiecesBean bean = piecesBeanSparseArray.get(i * 100 + y);
                    if (bean != null) {
                        return false;
                    }
                }
            }
        }

        if (y > targetY) {
            for (int i = targetY + 1; i < y; i++) {
                PiecesBean bean = piecesBeanSparseArray.get(x * 100 + i);
                if (bean != null) {
                    return false;
                }
            }
        } else {
            for (int i = y + 1; i < targetY; i++) {
                PiecesBean bean = piecesBeanSparseArray.get(x * 100 + i);
                if (bean != null) {
                    return false;
                }
            }
        }
        return true;
    }

    // 将
    private static boolean judgeTake(PiecesBean piecesBean, int targetKey, SparseArray<PiecesBean> piecesBeanSparseArray) {
        int key = piecesBean.getPointKey();
        int x = key / 100;
        int y = key % 100;
        int targetX = targetKey / 100;
        int targetY = targetKey % 100;
        if (x != targetX && y != targetY) {
            return false;
        }
        // 范围限制
        if (y <= 3) {
            if (targetY > 3) return false;
        } else {
            if (targetY < 8) return false;
        }
        if (targetX < 4 || targetX > 6) return false;
        // 移动判断
        if (x == targetX) {
            if (Math.abs(y - targetY) > 1) return false;
        } else {
            // 此时 v = targetY
            if (Math.abs(x - targetX) > 1) return false;
        }
        return true;
    }

    // 士
    private static boolean judgeScholar(PiecesBean piecesBean, int targetKey, SparseArray<PiecesBean> piecesBeanSparseArray) {
        int key = piecesBean.getPointKey();
        int x = key / 100;
        int y = key % 100;
        int targetX = targetKey / 100;
        int targetY = targetKey % 100;
        if (x == targetX || y == targetY) {
            return false;
        }
        // 范围限制
        if (y <= 3) {
            if (targetY > 3) return false;
        } else {
            if (targetY < 8) return false;
        }
        if (targetX < 4 || targetX > 6) return false;
        // 移动判断
        if (Math.abs(y - targetY) > 1 || Math.abs(x - targetX) > 1) return false;
        return true;
    }

    //卒
    private static boolean judgePawn(PiecesBean piecesBean, int targetKey, SparseArray<PiecesBean> piecesBeanSparseArray) {
        int key = piecesBean.getPointKey();
        int x = key / 100;
        int y = key % 100;
        int targetX = targetKey / 100;
        int targetY = targetKey % 100;
        if (piecesBean.getCampType() == PiecesBean.PARTY_A) {
            if (y <= 5) {
                // 己方阵营
                if (x != targetX) return false;
                if (targetY - y != 1) return false;
            } else {
                // 对方阵营
                if (targetY < y) return false;
                int differenceX = Math.abs(x - targetX);
                int differenceY = Math.abs(y - targetY);
                if (differenceX > 1) return false;
                if (differenceY > 1) return false;
                if (differenceX == differenceY) return false;
            }
        } else {
            if (y >= 6) {
                // 己方阵营
                if (x != targetX) return false;
                if (y - targetY != 1) return false;
            } else {
                // 对方阵营
                if (targetY > y) return false;
                int differenceX = Math.abs(x - targetX);
                int differenceY = Math.abs(y - targetY);
                if (differenceX > 1) return false;
                if (differenceY > 1) return false;
                if (differenceX == differenceY) return false;
            }
        }
        return true;
    }

    //马
    private static boolean judgeHorse(PiecesBean piecesBean, int targetKey, SparseArray<PiecesBean> piecesBeanSparseArray) {
        int key = piecesBean.getPointKey();
        int x = key / 100;
        int y = key % 100;
        int targetX = targetKey / 100;
        int targetY = targetKey % 100;
        if (x == targetX || y == targetY) {
            return false;
        }
        int differenceX = Math.abs(x - targetX);
        int differenceY = Math.abs(y - targetY);
        // 移动形状限制
        if (differenceX > 2 || differenceY > 2) return false;
        if (differenceX <= 1 && differenceY <= 1) return false;
        if (differenceX == 2 && differenceY != 1) return false;
        if (differenceY == 2 && differenceX != 1) return false;
        // 别马脚限制
        if (differenceX == 2) {
            if (x > targetX) {
                if (piecesBeanSparseArray.get((targetX + 1) * 100 + y) != null) {
                    return false;
                }
            } else {
                if (piecesBeanSparseArray.get((targetX - 1) * 100 + y) != null) {
                    return false;
                }
            }
        } else {
            // 此时 differenceY 一定等于 2
            if (y > targetY) {
                if (piecesBeanSparseArray.get(x * 100 + (targetY + 1)) != null) {
                    return false;
                }
            } else {
                if (piecesBeanSparseArray.get(x * 100 + (targetY - 1)) != null) {
                    return false;
                }
            }
        }
        return true;
    }

    //炮
    private static boolean judgeCannon(PiecesBean piecesBean, int targetKey, SparseArray<PiecesBean> piecesBeanSparseArray) {
        int key = piecesBean.getPointKey();
        int x = key / 100;
        int y = key % 100;
        int targetX = targetKey / 100;
        int targetY = targetKey % 100;
        if (x != targetX && y != targetY) {
            return false;
        }
        int count = 0;
        if (y == targetY) {
            if (x > targetX) {
                for (int i = targetX + 1; i < x; i++) {
                    PiecesBean bean = piecesBeanSparseArray.get(i * 100 + y);
                    if (bean != null) {
                        count++;
                    }
                }
            } else {
                for (int i = x + 1; i < targetX; i++) {
                    PiecesBean bean = piecesBeanSparseArray.get(i * 100 + y);
                    if (bean != null) {
                        count++;
                    }
                }
            }
        }
        if (x == targetX) {
            if (y > targetY) {
                for (int i = targetY + 1; i < y; i++) {
                    PiecesBean bean = piecesBeanSparseArray.get(x * 100 + i);
                    if (bean != null) {
                        count++;
                    }
                }
            } else {
                for (int i = y + 1; i < targetY; i++) {
                    PiecesBean bean = piecesBeanSparseArray.get(x * 100 + i);
                    if (bean != null) {
                        count++;
                    }
                }
            }
        }
        if (count > 1) {
            return false;
        }
        if (count == 1) {
            if (piecesBeanSparseArray.get(targetKey) == null) {
                return false;
            }
        } else {
            if (piecesBeanSparseArray.get(targetKey) != null) {
                return false;
            }
        }
        return true;
    }

    // 象
    private static boolean judgeAs(PiecesBean piecesBean, int targetKey, SparseArray<PiecesBean> piecesBeanSparseArray) {
        int key = piecesBean.getPointKey();
        int x = key / 100;
        int y = key % 100;
        int targetX = targetKey / 100;
        int targetY = targetKey % 100;
        if (Math.abs(x - targetX) != 2) return false;
        if (Math.abs(y - targetY) != 2) return false;
        // 范围问题
        if (y > 5) {
            if (targetY < 6) return false;
        } else {
            if (targetY > 5) return false;
        }
        // 象蹄子问题处理
        if (x > targetX) {
            if (y > targetY) {
                if (piecesBeanSparseArray.get((x - 1) * 100 + (y - 1)) != null) {
                    return false;
                }
            } else {
                if (piecesBeanSparseArray.get((x - 1) * 100 + (y + 1)) != null) {
                    return false;
                }
            }
        } else {
            if (y > targetY) {
                if (piecesBeanSparseArray.get((x + 1) * 100 + (y - 1)) != null) {
                    return false;
                }
            } else {
                if (piecesBeanSparseArray.get((x + 1) * 100 + (y + 1)) != null) {
                    return false;
                }
            }
        }
        return true;
    }


}
